import requests
from urllib.parse import quote
import time

base_url = "https://lf-cdn.trae.com.cn/obj/trae-com-cn/pkg/app/releases/stable/{version}/win32/Trae%20CN-Setup-x64.exe"

def check_version(url):
    try:
        response = requests.head(url, timeout=5)
        return response.status_code == 200
    except Exception as e:
        print(f"请求失败: {str(e)}")
        return False

def main():
    # 从1.0.12384开始递减
    major, minor, patch = 1, 0, 12491
    available_versions = []
    
    while patch > 0:  # 设置最小版本号限制
        version = f"{major}.{minor}.{patch}"
        encoded_version = quote(version, safe='')
        url = base_url.format(version=encoded_version)
        
        print(f"正在检测版本: {version}", end='\r', flush=True)
        
        if check_version(url):
            print(f"\n发现可下载版本: {version}")
            print(f"下载地址: {url}")
            available_versions.append(url)
            
        patch -= 1
        if patch < 12480:
            break
        time.sleep(0.5)  # 降低请求频率

    print("\n\n检测完成，以下是所有可下载版本：")
    for i, url in enumerate(available_versions, 1):
        print(f"{i}. {url}")

if __name__ == "__main__":
    print("开始检测可用版本...")
    main()